package com.kifas.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kifas.entity.BarangEntity;
import com.kifas.entity.JenisBarangEntity;
import com.kifas.entity.KontrakEntity;
import com.kifas.entity.KontrakPembelianEntity;
import com.kifas.entity.SatuanEntity;
import com.kifas.entity.SupplierEntity;
import com.kifas.model.xml.BarangModel;
import com.kifas.model.xml.JenisBarangModel;
import com.kifas.model.xml.KontrakModel;
import com.kifas.model.xml.KontrakPembelianModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.SatuanModel;
import com.kifas.model.xml.SupplierModel;
import com.kifas.service.EntityServiceManager;

@Component
@Scope("prototype")
@Path("/Kontraks")
public class KontrakController {

	private static Logger log = Logger.getLogger(KontrakController.class);

	@Context
	UriInfo uriInfo;

	@Autowired
	private EntityServiceManager entityManager;

	@GET
	@Produces(MediaType.TEXT_XML)
	public List<KontrakModel> getKontraks() {
		return transformList(entityManager.getKontraks());
	}

	@GET
	@Path("{id}")
	@Produces(MediaType.TEXT_XML)
	public KontrakModel getKontrak(@PathParam("id") Long idKontrak) {
		return transform(entityManager.getKontrak(idKontrak));
	}

	@GET
	@Path("Supplier/{id}")
	@Produces(MediaType.TEXT_XML)
	public List<KontrakModel> getKontrakBySupplier(@PathParam("id") Long idSupplier) {
		SupplierEntity supplier = entityManager.getSupplier(idSupplier);
		return transformList(entityManager.getKontraks(supplier));
	}

	@POST
	@Produces(MediaType.APPLICATION_XML)
	@Consumes(MediaType.APPLICATION_XML)
	public PersistStatus insertKontrak(JAXBElement<KontrakModel> xml) {
		KontrakModel model = xml.getValue();
		KontrakEntity kontrak = new KontrakEntity();
		SupplierEntity supplier = entityManager.getSupplier(model.getSupplier().getIdSupplier());
		kontrak.setIdKontrak(null);
		kontrak.setNomorKontrak(model.getNomorKontrak());
		kontrak.setTanggal(model.getTanggal());
		kontrak.setSupplier(supplier);
		Set<KontrakPembelianEntity> kontrakPembelians = new HashSet<KontrakPembelianEntity>();
		for (KontrakPembelianModel modelDetail : model.getKontrakPembelians()) {
			BarangEntity barang = entityManager.getBarang(modelDetail.getBarang().getIdBarang());
			KontrakPembelianEntity kontrakPembelian = new KontrakPembelianEntity();
			kontrakPembelian.setIdKontrakPembelian(null);
			kontrakPembelian.setBarang(barang);
			kontrakPembelian.setClosed(modelDetail.isClosed());
			kontrakPembelian.setKontrak(kontrak);
			kontrakPembelian.setQty(modelDetail.getQty());
			kontrakPembelian.setTanggalKirim(modelDetail.getTanggalKirim());
			kontrakPembelians.add(kontrakPembelian);
		}
		kontrak.setKontrakPembelians(kontrakPembelians);
		try {
			entityManager.persist(kontrak);
		} catch (Exception e) {
			log.error("can not persist kontrak", e);
			return new PersistStatus("KontrakEntity", "failed", "can not persist kontrak");
		}
		return new PersistStatus("KontrakEntity", "success");
	}

	private List<KontrakModel> transformList(List<KontrakEntity> kontraks) {
		List<KontrakModel> kontrakModels = new ArrayList<KontrakModel>();
		for (KontrakEntity kontrak : kontraks) {
			kontrakModels.add(transform(kontrak));
		}
		return kontrakModels;
	}

	private KontrakModel transform(KontrakEntity kontrak) {
		List<KontrakPembelianModel> kontrakPembelianModels = new ArrayList<KontrakPembelianModel>();
		List<KontrakPembelianEntity> kontrakPembelians = entityManager.getKontrakPembelians(kontrak);
		SupplierEntity supplierEntity = kontrak.getSupplier();
		SupplierModel supplierModel = new SupplierModel(supplierEntity.getIdSupplier(), supplierEntity.getNamaSupplier(), supplierEntity.getAlamat(),
				supplierEntity.getNoTelp(), supplierEntity.getNoFax(), supplierEntity.getContactPerson(), supplierEntity.getCreatedBy());
		for (KontrakPembelianEntity kontrakPembelian : kontrakPembelians) {
			BarangEntity barangEntity = kontrakPembelian.getBarang();
			SatuanEntity satuanEntity = barangEntity.getSatuan();
			SatuanModel satuanModel = new SatuanModel(satuanEntity.getIdSatuan(), satuanEntity.getSatuan(), satuanEntity.getCreatedBy());
			JenisBarangEntity jenisBarangEntity = barangEntity.getJenisBarang();
			JenisBarangModel jenisBarangModel = new JenisBarangModel(jenisBarangEntity.getIdJenisBarang(), jenisBarangEntity.getJenis(), jenisBarangEntity.getCreatedBy());
			BarangModel barangModel = new BarangModel(barangEntity.getIdBarang(), barangEntity.getKdBarang(), jenisBarangModel, satuanModel,
					barangEntity.getNama(), barangEntity.getHargaBeli(), barangEntity.getHargaJual(), barangEntity.getCreatedBy());
			KontrakPembelianModel kontrakModel = new KontrakPembelianModel(kontrakPembelian.getIdKontrakPembelian(), barangModel, kontrakPembelian.isClosed(),
					kontrakPembelian.getQty(), kontrakPembelian.getTanggalKirim(), kontrakPembelian.getNomorKontrakPembelian(), kontrakPembelian.getCreatedBy(),kontrakPembelian.getCreatedDate());
			kontrakPembelianModels.add(kontrakModel);
		}
		return new KontrakModel(kontrak.getIdKontrak(), kontrak.getNomorKontrak(), supplierModel, kontrak.getTanggal(), new HashSet<KontrakPembelianModel>(
				kontrakPembelianModels), kontrak.getCreatedBy(), kontrak.getCreatedDate());
	}
}
